home *** CD-ROM | disk | FTP | other *** search
- /****i* SOURCE_FILE/INFO
- *
- * NAME
- * DelimitedTextTable.js
- *
- * USAGE
- * Part of Netobjects JavaScript Library.
- *
- * COPYRIGHT
- * Copyright ⌐ 2000-2005 Website Pros, Inc.
- * All Rights Reserved.
- *
- * This is an unpublished work protected by Website Pros, Inc.
- * as a trade secret, and is not to be used or disclosed except as
- * expressly provided in a written license agreement executed by
- * you and Website Pros, Inc.
- *
- * <copyright@websitepros.com>
- *
- * NOTES
- * JavaScript code.
- *
- *****/
- if (!IS.isModuleInitialized("IS.NOF.TEXT.DelimitedTextTable"))
- {
-
- /****h* NOF_JavaScript_Library/NOF.TEXT.DelimitedTextTable
- *
- * NAME
- * NOF.TEXT.DelimitedTextTable
- *
- * DESCRIPTION
- *
- * <code>DelimitedTextTable</code> is a class which
- * Usage sample:
- * External dependencies: none.
- ****/
-
- /**
- * Constructor
- **/
- function TEXT_DelimitedTextTable(/*String*/ delimiter, /*String*/ lineSeparator) {
- this.__proto__ = TEXT_DelimitedTextTable.prototype;
-
- this.delimiter = delimiter;
- this.lineSeparator = (lineSeparator) ? lineSeparator : "\n";
- }
- {
- var method = TEXT_DelimitedTextTable.prototype;
-
- /**
- * parse a delimited file content and get the table as list of lines
- * containing the formated values
- * @param fileContent
- **/
- method.parse = function (/*string*/ fileContent) {
- if (fileContent == null || fileContent.length == 0) return null;
- //alert(fileContent);
-
- var lines = fileContent.split(this.lineSeparator);
- var l = lines.length;
- if (l == 0)
- return null;
-
- var tbl = new Array();
-
- var i = -1;
- var line, tmpStr;
- var candidateColValues;
- var iOf, j;
-
- while (++i < l) {
- line = lines[i];
- if (line.trim().length == 0) continue;
- //log.info("line " + i + " = " + line, "DelimitedTextTable", "parse");
- var colValues = new Array(); //line.split("\t");
-
- //parseLine()
- if (line.indexOf('""') > -1) {
- line = line.replaceSubstr('""', "__DQUOTE__");
- }
- candidateColValues = line.split(this.delimiter);
- //log.info("candidate columns " + candidateColValues.length, "DelimitedTextTable", "parse");
- for (j=0; j < candidateColValues.length; j++) {
- tmpStr = candidateColValue = candidateColValues[j];
- iOf = candidateColValue.indexOf('"');
- //log.info(candidateColValue, "DelimitedTextTable", "parse");
- //log.info("indexOf = " + iOf, "DelimitedTextTable", "parse");
- if ((iOf > -1) && (candidateColValue.indexOf('"', iOf + 1) < 0)) {
- while ((candidateColValues[++j].indexOf('"') < 0) && (j < candidateColValues.length)) {
- tmpStr += this.delimiter + candidateColValues[j];
- }
- tmpStr += this.delimiter + candidateColValues[j];
- }
- if (tmpStr.indexOf('"') == 0) {
- tmpStr = tmpStr.substr(1, tmpStr.length - 2);
- //tmpStr = tmpStr.substring(1, tmpStr.length - 1);
- }
- if (tmpStr.indexOf("__DQUOTE__") > -1) {
- tmpStr = tmpStr.replaceSubstr("__DQUOTE__", '\"');
- }
- //log.info("added = " + tmpStr, "DelimitedTextTable", "parse");
- colValues[colValues.length] = tmpStr;
- }
- //end parseLine
-
- tbl[tbl.length] = colValues;
-
- }
-
- return tbl;
- }
-
- }
-
- // add it to NOF.TEXT namespace
- NOF.TEXT.__proto__.DelimitedTextTable = TEXT_DelimitedTextTable;
- }